﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BAL.Entities;
using BAL.Components;
using BAL.Utils;

namespace DigitalAnts
{
    public partial class ProductPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public DataRow doGetProductByID()
        {
            clsProduct obj = new clsProduct();
            obj.ProductID = Convert.ToInt32(Request["id"]);
            return clsProductBAL.doProductSelectID(obj).Rows[0];
        }

        

        public void doUpdateCart(string cart)
        {
            if (HttpContext.Current.Session["Logged"] != null)
            {
                if (HttpContext.Current.Session["Logged"].Equals(true))
                {
                    clsUser obj = new clsUser();
                    obj.UserID = Convert.ToInt32(Session["UserID"]);
                    obj.Card = cart;
                    clsUserBAL.doUpdateUserCart(obj);
                }
            }
        }

        public void doUpdateFavorite(string favorite)
        {
            if (HttpContext.Current.Session["Logged"] != null)
            {
                if (HttpContext.Current.Session["Logged"].Equals(true))
                {
                    clsUser obj = new clsUser();
                    obj.UserID = Convert.ToInt32(Session["UserID"]);
                    obj.Favorite = favorite;
                    clsUserBAL.doUpdateUserFavorite(obj);
                }
            }
        }

        protected void lbtCart_Click(object sender, EventArgs e)
        {
            int productID = Int32.Parse(Request["id"]);
            int maxItem = 100;
            if (HttpContext.Current.Session["Logged"] != null)
            {
                if (HttpContext.Current.Session["Logged"].Equals(true))
                {
                    maxItem = 500;
                }
            }

            List<int> cart = (List<int>)Session["Cart"];
            if (cart.Count < maxItem)
            {
                cart = clsCartAndFavorite.addNewID(cart, productID);
                Session["Cart"] = cart;
                doUpdateCart(clsCartAndFavorite.getStringID(cart));
                Response.Redirect("~/User/UserCart.aspx");
            }
            else
            {
                Response.Redirect(Request.Url.ToString());
            }

        }

        protected void lbtLike_Click(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session["Logged"] != null)
            {
                if (HttpContext.Current.Session["Logged"].Equals(true))
                {
                    int productID = Int32.Parse(Request["id"]);
                    int maxItem = 500;


                    List<int> favorite = (List<int>)Session["Favorite"];
                    if (favorite.Count < maxItem)
                    {
                        favorite = clsCartAndFavorite.addNewID(favorite, productID);
                        Session["Favorite"] = favorite;
                        doUpdateFavorite(clsCartAndFavorite.getStringID(favorite));
                        Response.Redirect("~/User/FavoriteList.aspx");
                    }
                    else
                    {
                        Response.Redirect(Request.Url.ToString());
                    }
                }
                else
                {
                    Response.Redirect("Please login to add new favorite list");
                }
            }
            else
            {
                Response.Redirect("Please login to add new favorite list");
            }
        }
    }
}